﻿using System;
using System.Collections;
using System.Windows.Controls;
using jSLApp.Client.ApplicationBase;
using jSLApp.Client.MainService;
using jSLCore.Client.Presentation.BaseControl.Selection;

namespace jSLApp.Client.Forms
{
    public partial class SimpleUserList : SelectControl
    {
        public SimpleUserList()
        {
            InitializeComponent();
        }

        public override void LoadInitialData()
        {
           Search();
        }

        private void goto_Details(object sender, RowSelectedEventArgs e)
        {
            var user = (User)e.RowObject;
            MainContentContainer.Current.GotoUserAbout(user.Login);
        }

        private void Search()
        {
            DoWork("Поиск...", (sender, args) =>
                                   {
                                       args.Result = Communication.Proxy.GetUserListSimple(args.String);
                                   }, (o, eventArgs) =>
                                                          {
                                                              if(eventArgs.Error==null)
                                                              {
                                                                  grid.ItemsSource = (IEnumerable) eventArgs.Result;
                                                              }
                                                          }, txt.Text);   
        }

        #region dialog

        public override ISelectableGrid GetSelectableGrid()
        {
            return grid;
        }

        public override bool IsStandartOkCancelEnabledWhenNotSelectable
        {
            get { return true; }
        }

        public override bool IsOkVisibleWhenNotSelectable
        {
            get { return false; }
        }

        public override object CancelContentWhenNotSelectable
        {
            get { return "Закрыть"; }
        }

        protected override void AcceptButtonClicked()
        {
            Search();
        }

       

        public override Control GetControlForInitialFocus()
        {
            return txt;
        }

        #endregion
    }
}
